package com.study.a01mystream;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamDemo10 {
    public static void main(String[] args) {
        //collect(Collector collector)  收集流中的数据,放到list集合中

        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list,/*"张三-男-15",*/"张三-男-15","李四-男-16","王五-男-17","王六-男-18","王七-男-19","张无极-男-20","周芷若-女-15");

        List<String> list1 = list.stream()
                .filter(s->"男".equals(s.split("-")[1]))
                /*.forEach(s-> System.out.println(s));*/
                .collect(Collectors.toList());

        System.out.println(list1);

        System.out.println("----------------");

        //收集到HashSet集合中
        //!  去重!
        Set<String> list2 = list.stream().filter(s->"男".equals(s.split("-")[1]))
                .collect(Collectors.toSet());

        System.out.println(list2);

        System.out.println("----------------");

        //收集到Map集合中
        //键不能重复，否则会报错
        Map<String, Integer> map = list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(new Function<String, String>() {
                    @Override
                    public String apply(String s) {
                        return s.split("-")[0];

                    }
                }, new Function<String, Integer>() {
                    @Override
                    public Integer apply(String s) {
                        return Integer.parseInt(s.split("-")[2]);
                    }
                }));

        System.out.println(map);

        System.out.println("----------------");

        //lambda表达式
        Map<String, Integer> map1 = list.stream()
                .filter(s -> "男".equals(s.split("-")[1]))
                .collect(Collectors.toMap(s -> s.split("-")[0], s -> Integer.parseInt(s.split("-")[2])));

        System.out.println(map1);
    }
}
